package cn.fanqinghong.org.controller;

import cn.fanqinghong.basic.anno.PreAuthorize;
import cn.fanqinghong.basic.exception.BusinessException;
import cn.fanqinghong.basic.util.ExcelUtil;
import cn.fanqinghong.org.domain.ShopAuditLog;
import cn.fanqinghong.org.service.IShopService;
import cn.fanqinghong.org.domain.Shop;
import cn.fanqinghong.org.query.ShopQuery;
import cn.fanqinghong.basic.util.AjaxResult;
import cn.fanqinghong.basic.query.PageList;
import cn.fanqinghong.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @PreAuthorize(name = "店铺入驻",value = "shop:settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            //调用业务层代码
            shopService.selectment(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "店铺保存和修改",value = "shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除",value = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "店铺审核",value = "shop:audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            //调用业务层代码
            shopService.pass(log);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     *激活
     * @param id
     * @param response
     */
    @GetMapping("/activation/{id}")
    public void activation(@PathVariable("id")Long id,HttpServletResponse response){
        System.out.println(id);
        try {
            //1.修改店铺状态为---审核通过  @TODO   你们做
            Shop shop = shopService.queryById(id);
            shop.setState(2);
            shopService.update(shop);
            response.sendRedirect("http://localhost:8081/#/login");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 审核驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            //调用业务层代码
            shopService.reject(log);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 文件导出
     * @param response
     */
    @GetMapping("/export/excel")
    public void export(HttpServletResponse response){
        //调用业务层代码,查询全部数据
        List<Shop> shops = shopService.queryAll();
        //把数据导出到
        //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, boolean isCreateHeader, HttpServletResponse response

        ExcelUtil.exportExcel(shops,null,"店铺审核",Shop.class,"店铺入驻.xls",true,response);
    }

    /**
     * 导入报表
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart MultipartFile file){
        //MultipartFile file文件, Integer titleRows 标题行, Integer 头部行, Class<T> pojoClass
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        //批量新增
        shopService.savas(shops);
    }

    /**
     * 柱状图
     * @return
     */
    @PostMapping("/echarts/bar")
    public Map<String,Object> echartsBar(){
        //调用业务层查询到echarts根据状态分组的数据和数量
        List<ShopVo> voList = shopService.queryEcharts();
        //把状态的
        List<String> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }

}
